package com.etlmaster.datax.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.etlmaster.executor.utils.LogWriter;

public final class StringUtil {
	
	private static final String VARIABLE_PATTERN = "(\\$)\\{(\\w+)\\}";
	

	// 小数
	static Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");

	static Pattern IntegerPattern = Pattern.compile("-?[0-9]*$");

	private StringUtil(){
	}
	
	public static String replaceEnvironmentVariables(String text) {
		Pattern pattern = Pattern.compile(VARIABLE_PATTERN);
		Matcher matcher = pattern.matcher(text);
		
		while(matcher.find()){
			LogWriter.addLog("DEBUG","replace " + matcher.group(2) + 
					" with " + System.getenv(matcher.group(2)));
			
			text = StringUtils.replace(text, matcher.group(),
					StringUtils.defaultString(System.getenv(matcher.group(2)), matcher.group()));
		}
		return text;
	}
	// 如果是整数时会返回false,增加整数模式判断
		public static boolean isNumberic(String text) {
			if (text.equals("") || text == null) {
				return false;
			}
			if (text.indexOf(".") > 0) {
				return pattern.matcher(text.replace(" ", "")).matches();
			} else {
				return IntegerPattern.matcher(text.replace(" ", "")).matches();
			}
		}
}
